package com.xyz.musicplayer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import beans.Songer;
import beans.User;
import utils.SongerAdapter;

public class ShowHistoryActivity extends AppCompatActivity {

    //声明引用
    Intent intent;
    ImageView ivForUserImage;
    ListView lvForHistoryRecord;
    TextView tvForUsername,tv1;
    User userAtPresent;
    SharedPreferences shared;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showhistoryrecord);

        init();

        //操作栏添加导航按钮
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch ( item.getItemId() ){
            case android.R.id.home:{
                finish();
            }break;
            default:
                Toast.makeText(this, "返回", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    private void init(){
        //实例化控件
        ivForUserImage = (ImageView) findViewById(R.id.imageView);
        lvForHistoryRecord = (ListView) findViewById(R.id.lv_records);
        tvForUsername = (TextView) findViewById(R.id.tv_100);
        tv1 = (TextView) findViewById(R.id.tv_101);

        //获取来自父界面的Intent
        intent = getIntent();
        userAtPresent = (User) intent.getSerializableExtra("user");

        //设置textview内容
        tvForUsername.setText(userAtPresent.getUsername());
        tvForUsername.setTextSize(50);
        tvForUsername.setTextColor(Color.BLUE);

        List<Songer> songerList = new ArrayList<>();

        //读取文件信息
        String fileName = userAtPresent.getUsername()+".history.xml";
        File file = new File("/data/data/com.xyz.musicplayer/shared_prefs/"+fileName);
        if ( !file.exists() ){
            //文件不存在
            Toast.makeText(this, "该用户初次登录，尚未由本地浏览记录", Toast.LENGTH_SHORT).show();
        }else {
            //文件存在
            shared = getSharedPreferences(fileName,MODE_PRIVATE);
            String getCount = shared.getString("total", "-1");
            int count = Integer.parseInt(getCount);
            for ( int i=0; i<5; i++ ){
                String record = shared.getString("histroy"+i,"过火---张信哲");
                String[] afterSplitRecord = record.split("---");
                Songer songer = new Songer(afterSplitRecord[0],afterSplitRecord[1]);
                songerList.add(songer);
            }
        }

        //定义适配器
        SongerAdapter adapter = new SongerAdapter(ShowHistoryActivity.this,R.layout.history_item,songerList);
        //使用自定义适配器
        lvForHistoryRecord.setAdapter(adapter);

        lvForHistoryRecord.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });


    }


}